package com.quark.web.ui.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.quark.data.type.QuarkPair;
import com.quark.framework.utility.QuarkLog;
import com.quark.framework.utility.QuarkUtilStringTools;

public class QuarkUiElementBase {

	String elementId = "";
	String elementTag = "";
	String elementName = "";
	String elementClass = "";
	String elementStyle = "";
	String elementInnerHtml = "";
	
	private Map<String, String> attributeNode = new HashMap<String, String>();
	private String elementHtml1 = ""; // "<tag name class style"
	private String elementHtml2 = ""; // ">"
	private String elementHtml3 = ""; // </tag> if needed
	protected List<String> triggerFuncs = new ArrayList<String>();
	protected List<String> watcherFuncs = new ArrayList<String>();
	
	public QuarkUiElementBase(String tag, String id, String name, String cssClass, String style, boolean noEndTag)
	{
		elementId = id;
		elementName = name;
		elementClass = cssClass;
		elementStyle = style;
		elementHtml2 = noEndTag ? "/>" : ">";
		elementHtml3 = noEndTag ? "" : "</" + tag + ">";
		elementTag = tag;
		
		
	}
	
	public String getElementOpenTag()
	{
		elementHtml1 = "<" + elementTag + " ";
		if(!QuarkUtilStringTools.isNullOrEmpty(elementId))
		{
			elementHtml1 += " id=\"" + elementId + "\" ";
		}
		if(!QuarkUtilStringTools.isNullOrEmpty(elementName))
		{
			elementHtml1 += " name=\"" + elementName + "\" ";
		}
		if(!QuarkUtilStringTools.isNullOrEmpty(elementClass))
		{
			elementHtml1 += " class=\"" + elementClass + "\"";
		}
		if(!QuarkUtilStringTools.isNullOrEmpty(elementStyle))
		{
			elementHtml1 += " style=\"" + elementStyle + "\"";
		}
		return elementHtml1;
	}
	
	public String getElementOpenTagEnd()
	{
		return elementHtml2;
	}
	
	public String getElementCloseTag()
	{
		return elementHtml3;
	}
	
	public void addCssClass(String css)
	{
		elementClass += " " + css;
	}
	
	public void addAttribute(String name, String value)
	{
//		if(attributeNode.containsKey(name))
//		{
//			QuarkLog.logMessage("addAttribute: duplicate key \"" + name + "\", old value=" + attributeNode.get(name) + ", new value=" + value);
//		}
		attributeNode.put(name, value);
	}
	
	public String getAttribute(String name)
	{
		return attributeNode.get(name);
	}
	
	public void removeAttribute(String name)
	{
		if(attributeNode.containsKey(name))
		{
			attributeNode.remove(name);
		}
	}
	
	public String createElement()
	{		
		String attribute = "";
		for(int i = watcherFuncs.size() - 1; i >= 0; i --)
		{
			attribute += watcherFuncs.get(i);
		}

		addAttribute("execute_1", attribute);
		attribute = "";
		for(int i = triggerFuncs.size() - 1; i >= 0; i --)
		{
			attribute += triggerFuncs.get(i);
		}

		addAttribute("execute_2", attribute);
		attribute = "";
		Iterator<String> it = attributeNode.keySet().iterator();
		while(it.hasNext())
		{
			String name = it.next();
			attribute += " " + name + "=\"" + attributeNode.get(name) + "\" ";
		}
		return getElementOpenTag()
		+ attribute
		+ getElementOpenTagEnd()
		+ elementInnerHtml
		+ getElementCloseTag();
	}
	
	public void addEventWatcher(String elementName, String event, String jscode)
	{
		String watcher = "Environment.addEventWatcher('" + elementName + "', callObj, '" + event + "', " + jscode + ");";
		watcherFuncs.add(watcher);
	}
	
	public void addEventCallback(String event, String jscode)
	{
		String callback = "Environment.addEventCallback(callObj, '" + event + "', " + jscode +");"; 
		triggerFuncs.add(callback);
	}
	
}
